#include "trancewidget.h"

TranceWidget::TranceWidget(QWidget *parent)
    :QWidget (parent)
{
    this->init_window();
    this->init_control();
    this->init_connect();
    this->trancevideo = new TranceVideo;
    this->video_cover = "";
    this->x=0;
}

TranceWidget::~TranceWidget()
{

}

void TranceWidget::init_window()
{
    this->setWindowTitle("转码界面");
    this->resize(700,700);
}

void TranceWidget::init_control()
{
    this->title = new QLabel("转码界面", this);
    this->title->move(250,50);
    this->title->setStyleSheet("font-size:36px");
    this->inputDate = new QLabel("请输入日期", this);
    this->inputDate->move(100,125);
    this->inputDate->setStyleSheet("font-size:20px");

    this->format = new QLabel("转码格式", this);
    this->format->move(100,350);
    this->path = new QLabel("路径存储", this);
    this->path->move(100,450);

    this->enterBtn = new QPushButton("确定", this);
    this->enterBtn->move(200,600);

    this->backBtn = new QPushButton("返回", this);
    this->backBtn->move(400,600);

    this->box1 = new QComboBox(this);
    this->box1->move(250,340);
    this->box1->setStyleSheet("width:220px;height:30px");
    this->box1->addItem("MP4");
    this->box1->addItem("MOV");
    this->box1->addItem("WMV");
    this->box1->addItem("FLV");



    this->pathEdit = new QLineEdit(this);
    this->pathEdit->move(250,450);
    this->pathEdit->setStyleSheet("width:180px;height:30px");
    this->pathEdit->setReadOnly(true);

    this->choosePathBtn = new QPushButton("+", this);
    this->choosePathBtn->move(475,450);
    this->choosePathBtn->setFixedSize(35,35);

    this->calendarBtn = new QPushButton("使用日期控件", this);
    this->calendarBtn->move(250,115);
    this->calendarBtn->setStyleSheet("width:200px");


    this->listwidget = new QListWidget(this);
    this->listwidget->move(120,150);
    this->listwidget->setFixedSize(440,160);

    this->calendar = new QCalendarWidget(this);
    this->calendar->move(300,125);
    this->calendar->hide();

    //获取该用户的视频
    char **qres=nullptr;
    int col = 0,row=0;
    int res = VideoModel::getInstance()->selectVideo(qres, row, col);
    if(res==0){
        for (int i=1;i<=row;i++) {
            qDebug() << i;

            this->listwidgetItem = new QListWidgetItem(QIcon(qres[i*col+1]), "");
            this->listwidgetItem->setData(Qt::UserRole, QString(qres[i*col]));
            qDebug() <<  this->listwidgetItem->data(Qt::UserRole).toString();
            this->listwidget->addItem(this->listwidgetItem);
        }

//        this->listwidget->setViewMode(QListView::IconMode);
//        this->listwidget->setIconSize(QSize(200,200));
//        this->listwidget->setResizeMode(QListWidget::Adjust);
//        this->listwidget->setMovement(QListWidget::Static);
//        this->listwidget->setHorizontalScrollBarPolicy(Qt::ScrollBarAlwaysOn);
    }

    this->listwidget->setViewMode(QListView::ListMode);
    this->listwidget->setResizeMode(QListWidget::Adjust);
    this->listwidget->setMovement(QListWidget::Static);
    this->listwidget->setIconSize(QSize(220,150));
    this->listwidget->setFlow(QListView::LeftToRight);

    this->Bar = new QProgressBar(this);
    this->Bar->setRange(0,100);
    this->Bar->setValue(0);
    this->Bar->move(250,520);
    this->Bar->setFixedWidth(300);

    this->timer = new QTimer(this);
}

void TranceWidget::init_connect()
{
    connect(this->calendarBtn, SIGNAL(clicked()), this, SLOT(showCalendar()));
    connect(this->calendar, SIGNAL(selectionChanged()), this, SLOT(on_calendarWidget_selectionChanged()));
    connect(this->choosePathBtn, SIGNAL(clicked()), this, SLOT(chooseDir()));
    connect(this->backBtn, SIGNAL(clicked()), this, SIGNAL(backToMain()));
    connect(this->listwidget, SIGNAL(itemDoubleClicked(QListWidgetItem*)), this, SLOT(chooseVideo(QListWidgetItem*)));
    connect(this->enterBtn, SIGNAL(clicked()), this, SLOT(doTrance()));

    //

//    connect(this->enterBtn, SIGNAL(clicked()), this, SLOT(startProress()));send
}

void TranceWidget::closeEvent(QCloseEvent *event)
{
    QMessageBox::StandardButton resBtn = QMessageBox::question(this, "Confirm Exit",
                tr("Are you sure you want to exit?\n"),
                QMessageBox::Cancel | QMessageBox::No | QMessageBox::Yes,
                QMessageBox::Yes);

    if (resBtn != QMessageBox::Yes) {
        event->ignore();
    } else {
//        UserModel::getInstance()->updateUserAll();
        event->accept();
    }
}

void TranceWidget::showCalendar()
{
    this->calendar->show();
}

void TranceWidget::on_calendarWidget_selectionChanged()
{
    QString ss = this->calendar->selectedDate().toString("yyyy-MM-dd");
    qDebug() << ss;
    this->calendarBtn->setText(this->calendar->selectedDate().toString());
    this->calendar->hide();

    //    this->listwidget->clear();

    this->listwidget->clear();
    //查询日期

//    //获取该用户的视频
    char **qres=nullptr;
    int col = 0,row=0;
    int res = VideoModel::getInstance()->selectVideo(qres, row, col);
    if(res==0){
        for (int i=1;i<=row;i++) {
            //如果是这个日期
            qDebug() << qres[i*col+2];
            if(QString(qres[i*col+2]).contains(ss))
            {
                this->listwidgetItem = new QListWidgetItem(QIcon(qres[i*col+1]), "");
                this->listwidgetItem->setData(Qt::UserRole, QString(qres[i*col]));
                qDebug() <<  this->listwidgetItem->data(Qt::UserRole).toString();
                this->listwidget->addItem(this->listwidgetItem);
            }

        }
        this->listwidget->setViewMode(QListView::ListMode);
        this->listwidget->setResizeMode(QListWidget::Adjust);
        this->listwidget->setMovement(QListWidget::Static);
        this->listwidget->setIconSize(QSize(220,150));
        this->listwidget->setFlow(QListView::LeftToRight);
    }
}

void TranceWidget::chooseDir()
{
    this->folderPath = new QFileDialog(this);
    this->folderPath->setFileMode(QFileDialog::Directory);
    this->folderPath->exec();
    auto selectDir = this->folderPath->selectedFiles();
    if (selectDir.size()>0)
    {
           qDebug() << "Dir Path:" << selectDir.at(0);
           this->pathEdit->setText(selectDir.at(0));
    }
}

void TranceWidget::chooseVideo(QListWidgetItem *video)
{
    video_cover = video->data(Qt::UserRole).toString();
    qDebug() << video_cover;
}

void TranceWidget::doTrance()
{
    if(video_cover=="")
    {
        QMessageBox::warning(this, "转码界面", "请先选择视频");
        return ;
    }
    this->trancevideo->openH264File(this->video_cover);

    char **qres;
    int row=0,col=0;
    VideoModel::getInstance()->selectFps(video_cover, qres, row, col);
    this->Bar->setRange(0,atoi(qres[1]));
    qDebug() << atoi(qres[1]);

    //获取当前日期和时间
    QDateTime currentDateTime = QDateTime::currentDateTime();
    //获取当前时间
    QTime currentTime = currentDateTime.time();
    //获取当前日期
    QDate currentDate = currentDateTime.date();
    QString date = currentDateTime.toString("yyyy_MM_dd_hh_mm_ss");
    QString datetime = currentDateTime.toString("yyyy-MM-dd hh:mm:ss");
    this->tran_cover = QString(this->pathEdit->text()+"/"+date+"."+this->box1->currentText());
    qDebug() << "this->tran_cover =" << this->tran_cover;


    connect(this->trancevideo, SIGNAL(sendS()), this, SLOT(updateProgress()));
    this->trancevideo->tranceToMp4(this->tran_cover);


    qres = nullptr;
    col=0,row=0;

    UserModel::getInstance()->selectUserId(qres, row, col);
    QString name;
    if(atoi(qres[1])==1)
    {
        name = "100101";
    }
    else if(atoi(qres[1])==2) {
        name = "100102";
    }
    else {
        name = "100103";
    }
    qres = nullptr;
    col=0,row=0;
    ConfigureModel::getInstance()->selectLastConfigure(qres, row, col);
    LogModel::getInstance()->InsertLog(datetime, qres[1], name, "导出");



}

void TranceWidget::startProgress()
{
    this->timer->start(100);
    connect(this->timer, SIGNAL(timeout), this, SLOT(updateProgress()));
}

void TranceWidget::updateProgress()
{
    this->x++;
    if(this->x >= Bar->maximum()){
        //Bar->setValue(value+10);
        this->x=0;
    }
    this->Bar->setValue(x+2);
}

